/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import org.eclipse.core.resources.IFolder;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
 * This page allows to get the following information in order to create a new belief:
 *  - Belief type
 *  - Belief name
 *  - Belief class
 *  - Belief optional attributes
 *  
 *  @author Jorge Valhondo Rama
 */

public class AddBeliefWizardPage1 extends WizardPage {
	private Spinner prioritySpinner;
	private Text packageNameText;
	
	private Combo typeCombo;
	private Text nameText;
	private Text classNameText;
	
	private Text descriptionText;
	private Spinner upDateRateSpinner;
	private Combo transientCombo;
	private Text evaluationModeText;
	

	
	/**
	 * Constructor for AddBeliefWizardPage1
	 * 
	 * @param pageName
	 */
	public AddBeliefWizardPage1(ISelection selection) {
		super("wizardPage");
		setTitle("Add Belief Wizard");
		setDescription("Page 1: Belief type and details");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(2, true);
		container.setLayout(layout);
		layout.horizontalSpacing = 50;
		layout.verticalSpacing = 4;
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addBeliefWizard");
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("&Type:");
		typeCombo = new Combo(container, SWT.READ_ONLY);
		typeCombo.setItems (new String [] {"belief","beliefset"});
		typeCombo.select(0);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		typeCombo.setLayoutData(gd);
		typeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		typeCombo.setToolTipText("select the type for the new belief");

		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("&Name:");
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(gd);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		nameText.setToolTipText("choose a name for the new belief");
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("&Class name:");
		classNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		classNameText.setLayoutData(gd);
		classNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label separator1 = new Label(container, SWT.NULL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		separator1.setLayoutData(gd);
		gd.horizontalSpan = 6;
		
		Label optionals =  new Label(container, SWT.NULL);
		optionals.setText("&Optionals attributes:");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 4;
		
		Label separator2 = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		separator2.setLayoutData(gd);
		gd.horizontalSpan = 4;
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("&Description:");
		descriptionText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		descriptionText.setLayoutData(gd);
		descriptionText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("&Up date rate:");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		upDateRateSpinner = new Spinner(container, SWT.BORDER);
		upDateRateSpinner.setIncrement(1);
		upDateRateSpinner.setPageIncrement(1);
		upDateRateSpinner.setSelection(0);
		upDateRateSpinner.setLayoutData(gd);
		upDateRateSpinner.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText("&Transient:");
		transientCombo = new Combo(container, SWT.READ_ONLY);
		transientCombo.setItems (new String [] {"false", "true"});
		transientCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		transientCombo.setLayoutData(gd);
		transientCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label7 = new Label(container, SWT.NULL);
		label7.setText("&Evaluation mode:");
		evaluationModeText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		evaluationModeText.setLayoutData(gd);
		evaluationModeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label8 = new Label(container, SWT.NULL);
		label8.setText("&Package Name:");
		packageNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		packageNameText.setLayoutData(gd);
		packageNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		initialize();
		dialogChanged();
		setControl(container);
	}
	
	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	/*
	 * This method is called in order to initialize the controls status
	 */
	private void initialize() {
		enabledPropertiesChanged();
	}
	
	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged(){
				
	}
	
	/*
	 * Ensures that all enabled text fields are set.
	 */
	private void dialogChanged() {
			
			if(nameText.getText().length() == 0 & classNameText.getText().length() == 0){
				updateStatus("A name and a class name must be specified for the new Belief/set");
				return;
			}
			
			if(nameText.getText().length() == 0){
				updateStatus("A name must be specified for the new Belief/set");
				return;
			}
			
			if(classNameText.getText().length() == 0){
				updateStatus("A class name must be specified for the new Belief/set");
				return;
			}
			
		updateStatus(null);
	}
		
	/**
	 * @return the belief name
	 */
	public String getBeliefName(){
		return nameText.getText();
	}
	
	/**
	 * @return the belief priority
	 */
	public String getBeliefPriority(){
		return prioritySpinner.getText();
	}
	
	/**
	 * 
	 * @return the class name for the new plan
	 */
	public String getBeliefClassName(){
		return classNameText.getText();
	}
	
	/**
	 * @return the belief type
	 */
	public String getBeliefType(){
		return typeCombo.getText();
	}
	
	
	/**
	 * 
	 * @return the package name
	 */
	public String getPackageName() {
		return packageNameText.getText();
	}
	
	/**
	 * 
	 * @return the belief description
	 */
	public String getBeliefDescription(){
		return descriptionText.getText();
	}
	
	/**
	 * 
	 * @return the belief up date rate
	 */
	public String getBeliefUpDateRate(){
		return upDateRateSpinner.getText();
	}
	
	/**
	 * 
	 * @return the belief transient
	 */
	public String getBeliefTransient(){
		return transientCombo.getText();
	}
	
	/**
	 * 
	 * @return the belief evaluation mode
	 */
	public String getBeliefEvaluationMode(){
		return evaluationModeText.getText();
	}
	
	/**
	 * 
	 * @return the agent source folder
	 */
	public IFolder getSourceFolder(){ //esto me da null pointer, creo que no me es necesario.
		/**
		try{
			return getAgentFile().getProject().getFolder("src");
		}
		catch(Exception e1){
			e1.printStackTrace();
		}
		*/
		return null;
	}
}